#include <util.h>

string resolveName(string varName, ScopeStack_t & stack) {
	if (stack.empty())
		return varName;
	
    string retName = varName;
    string & scopeName = stack.front();
    retName = scopeName + "::" + retName;
	return retName;
}

PDTNode * checkName(string varName, ScopeStack_t & stack, Name2Node_t & name2node, bool & isFound) {
    isFound = false;
    if (stack.empty()) {
        Name2Node_t::iterator itr = name2node.find(varName);
        if (itr != name2node.end()) { // not found
            // found
            isFound = true;
            return (*itr).second;
        }
    }
    ScopeStack_t::iterator it = stack.begin();
    while (it != stack.end()) {
        string temp = *it + "::" + varName;
        Name2Node_t::iterator itr = name2node.find(temp);
        if (itr == name2node.end()) { // not found
        	it++;
            continue;
        }

        // found
        isFound = true;
        return (*itr).second;
    }
    assert(isFound == false);
    // if we are here then the varName is not in a scope, it may be global, try it without a prefix
    Name2Node_t::iterator itr = name2node.find(varName);
    if (itr != name2node.end()){
        isFound = true;
        return (*itr).second;
    }
    return NULL;
}

void updateAntlrProps(PDTNode & node, PDTANTLRProp_t & prop_struct) {
    node.getProperty().setAllData(prop_struct);
}

PDTANTLRProp_t createAntlrProp(unsigned int line, int charPosition, long long start, long long stop) {
    PDTANTLRProp_t properties;
    properties.line = line;
    properties.charPosition = charPosition;
    properties.start = start;
    properties.stop= stop;
    return properties;
}
